const axios = require('axios');
axios.defaults.timeout = 5000;//5秒无响应表示请求超时
class AxiosModules {
    //淘宝
    static async getAxios(url, parmas) {
        let message = {};
        await axios.get(url, parmas)
            .then(function (response) {
                if (response.data.errcode != undefined) {
                    if (response.data.errcode != 0) {
                        message = { code: response.data.errcode, msg: response.data.errmsg, data: {} };
                    } else {
                        message = { code: 1000, msg: 'success', data: {} };
                    }

                } else {
                    message = { code: 1000, msg: 'success', data: response.data };
                }
            })
            .catch(function (error) {
                message = { code: -2, msg: 'service exception', data: {} };
            });
        return message;
    }
    static async postAxios(url, parmas) {
        let message = {};
        await axios.post(url, parmas, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
            .then(function (response) {
                if (response.data.errcode != undefined) {
                    if (response.data.errcode != 0) {
                        message = { code: response.data.errcode, msg: response.data.errmsg, data: {} };
                    } else {
                        message = { code: 1000, msg: 'success', data: {} };
                    }

                } else {
                    message = { code: 1000, msg: 'success', data: response.data };
                }
            })
            .catch(function (error) {
                message = { code: -2, msg: 'service exception', data: {} };
            });
        return message;
    }
    //公用
    static async jdGetAxios(url, parmas) {
        let message = {};
        await axios.get(url, {
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            params: parmas
        })
            .then(function (response) {
                //console.log(response)
                message = response.data;
            })
            .catch(function (error) {
                //console.log(error)
                message = { code: -2, msg: 'service exception', data: {} };
            });
        return message;
    }
    static async jdPostAxios(url, parmas) {
        let message = {};
        await axios.post(url, parmas, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
            .then(function (response) {
                message = response.data;
            })
            .catch(function (error) {
                message = { code: -2, msg: 'service exception', data: {} };
            });
        return message;
    }
}
module.exports = AxiosModules;